#include "stdio.h"
#include "SDL/SDL.h"

int main(int argc, char *argv[]) {

SDL_Surface *screen; //This pointer will reference the backbuffer
SDL_Surface *mario; //This pointer will reference our bitmap sprite
SDL_Surface *temp;//This pointer will temporarily reference our bitmap  sprite
SDL_Rect src, dest, pos;//These rectangles will describe the source and destination regions of our blit
SDL_Event event;
SDL_Surface *pSuper;
SDL_Surface *super;
int done = 1, i;
//We must first initialize the SDL video component, and check for success
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
//Set the video mode to fullscreen 640x480 with 16bit colour and double- buffering
screen = SDL_SetVideoMode(798, 601, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
//Load the bitmap into a temporary surface, and check for success
temp = SDL_LoadBMP("img/mario.bmp");
if (temp == NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
//Convert the surface to the appropriate display format
mario = SDL_DisplayFormat(temp);

pSuper = SDL_LoadBMP("img/super.bmp");
super = SDL_DisplayFormat(pSuper);
SDL_FreeSurface(pSuper);

//Release the temporary surface
SDL_FreeSurface(temp);
//Construct the source rectangle for our blit
src.x = 0;
src.y = 0;
src.w = mario->w; //Use image->w to display the entire width of the image
src.h = mario->h; //Use image->h to display the entire height of the image
//Construct the destination rectangle for our blit
dest.x = 0;//Display the image at the (X,Y) coordinates (100,100)
dest.y = 0;
dest.w = mario->w;
dest.h = mario->h;


pos.x = 100;
pos.y = 100;
pos.w = super->w;
pos.h = super->h;

SDL_SetColorKey(super, SDL_SRCCOLORKEY, SDL_MapRGB(super->format, 255, 0, 255));

/*
//Blit the image to the backbuffer
SDL_BlitSurface(image, &src, screen, &dest);
//Flip the backbuffer to the primary Hardware Video Memory
SDL_Flip(screen);
//Release the surface
SDL_FreeSurface(image);
*/

while(done)
{
    //Blit the image to the backbuffer
    SDL_BlitSurface(mario, &src, screen, &dest);
    SDL_BlitSurface(super, NULL, screen, &pos);
    //Flip the backbuffer to the primary Hardware Video Memory
    SDL_Flip(screen);

    while (SDL_PollEvent(&event))
    {
    // check for messages
        switch (event.type)
        {
        // exit if the window is closed
        case SDL_QUIT:
        done = 0;
        break;
        // check for keypresses
        case SDL_KEYDOWN:
        {
            //helico up if SDLK_UP
            if(event.key.keysym.sym == SDLK_UP)
            {
                pSuper = SDL_LoadBMP("img/superup.bmp");
                super = SDL_DisplayFormat(pSuper);
                SDL_FreeSurface(pSuper);
                SDL_SetColorKey(super, SDL_SRCCOLORKEY, SDL_MapRGB(super->format, 255, 255, 255));
                pos.y -= 50;
                SDL_BlitSurface(super, NULL, screen, &pos);
            }
            //helico down if SDLK_down
            if(event.key.keysym.sym == SDLK_DOWN)
            {
                pSuper = SDL_LoadBMP("img/superdown.bmp");
                super = SDL_DisplayFormat(pSuper);
                SDL_FreeSurface(pSuper);
                SDL_SetColorKey(super, SDL_SRCCOLORKEY, SDL_MapRGB(super->format, 255, 0, 255));
                if(pos.y <= (mario->h - super->w-100))
                pos.y += 50;
                SDL_BlitSurface(super, NULL, screen, &pos);
            }
            //helico right if SDLK_RIGHT
            if(event.key.keysym.sym == SDLK_RIGHT)
            {
                pSuper = SDL_LoadBMP("img/superright.bmp");
                super = SDL_DisplayFormat(pSuper);
                SDL_FreeSurface(pSuper);
                SDL_SetColorKey(super, SDL_SRCCOLORKEY, SDL_MapRGB(super->format, 255, 255, 255));
                if(pos.x <= (mario->w - super->w))

                pos.x += 50;

                SDL_BlitSurface(super, NULL, screen, &pos);
            }
            //helico left if SDLK_LEFT
            if(event.key.keysym.sym == SDLK_LEFT)
            {
                pSuper = SDL_LoadBMP("img/superleft.bmp");
                super = SDL_DisplayFormat(pSuper);
                SDL_FreeSurface(pSuper);
                SDL_SetColorKey(super, SDL_SRCCOLORKEY, SDL_MapRGB(super->format, 255, 255, 255));
                pos.x -= 50;
                SDL_BlitSurface(super, NULL, screen, &pos);
            }
        // exit if ESCAPE is pressed
        if (event.key.keysym.sym == SDLK_ESCAPE)
        done = 0;
        break;
        }
        } // end switch
    } // end of message processing
}
SDL_FreeSurface(mario);
return 0; //Return success!
}
